﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;

namespace ESecretary.MyCommon
{
    /// <summary>
    /// 加密类
    /// </summary>
    public sealed class Encryption
    {
        #region MD5加密，一般用于保存用户密码
        /// <summary>
        /// MD5加密，一般用于保存用户密码
        /// </summary>
        /// <param name="strSrc"></param>
        /// <returns></returns>
        public static string EncriptMD5(string strSrc)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            string strReturn = Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(strSrc)));
            return strReturn;
        }
        #endregion

        #region 加密文件
        /// <summary>
        /// 加密文件
        /// </summary>
        /// <param name="strKey">密钥</param>
        /// <param name="strFilePath">文件全路径</param>
        /// <returns></returns>
        public static bool EncritpFile(string strKey, string strFilePath)
        {
            bool blnFlag = false;
            RC2 rc2 = RC2.Create();
            rc2.Key = GetRC2Key(strKey);
            rc2.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            FileStream file = new FileStream(strFilePath, FileMode.Truncate);
            CryptoStream cs = new CryptoStream(file, rc2.CreateEncryptor(), CryptoStreamMode.Write);
            //cs.Write(

            return blnFlag;
        }
        #endregion

        #region 解密文件
        /// <summary>
        /// 解密文件
        /// </summary>
        /// <param name="strKey">密钥</param>
        /// <param name="strFilePath">文件全路径</param>
        /// <returns></returns>
        public static bool DecriptFile(string strKey, string strFilePath)
        {
            bool blnFlag = false;

            return blnFlag;
        }
        #endregion

        #region 对称加密DES
        /// <summary>
        /// 对称加密DES
        /// </summary>
        /// <param name="strKey">密钥</param>
        /// <param name="strSrc">加密字串</param>
        /// <returns></returns>
        public static string EncriptDES(string strKey, string strSrc)
        {
            if (strSrc == null)
            {
                return null;
            }
            DES des = new DESCryptoServiceProvider();// DESCryptoServiceProvider();
            des.Key = GetDESKey(strKey);
            des.IV = des.Key;
            MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            byte[] bTxt = Encoding.UTF8.GetBytes(strSrc);

            cs.Write(bTxt, 0, bTxt.Length);
            cs.Flush();
            cs.FlushFinalBlock();//不能少！！！y5mIPYebHkc=  y5mIPYebHkc=

            byte[] bEncrypt = ms.ToArray();
            string strResult = Convert.ToBase64String(bEncrypt);
            cs.Close();
            ms.Close();
            return strResult;
        }
        #endregion

        #region 对称解密DES
        /// <summary>
        /// 对称解密
        /// </summary>
        /// <param name="strKey">密钥</param>
        /// <param name="strSrc">解密字串</param>
        /// <returns></returns>
        public static string DecriptDES(string strKey, string strSrc)
        {
            if (strSrc == null)
            {
                return null;
            }
            DES des = new DESCryptoServiceProvider();
            des.Key = GetDESKey(strKey);
            des.IV = des.Key;

            MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

            byte[] bTemp = Convert.FromBase64String(strSrc);
            string strResult = null;
            try
            {
                cs.Write(bTemp, 0, bTemp.Length);
                cs.FlushFinalBlock();
                byte[] bTxt = ms.ToArray();
                strResult = Encoding.UTF8.GetString(bTxt);
            }
            catch
            {
                throw new Exception("密钥不对");
            }
            finally
            {
                cs.Close();
                ms.Close();
            }
            return strResult;
        }
        #endregion

        #region 获得DES加密所需定长64位密钥
        /// <summary>
        /// 获得DES加密所需定长64位密钥
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static byte[] GetDESKey(string str)
        {
            byte[] bKey = new byte[8];
            byte[] bTemp = Encoding.UTF8.GetBytes(str);

            for (int i = 0; i < 8; i++)
            {
                if (i <= bTemp.Length - 1)
                    bKey[i] = bTemp[i];
                else
                    bKey[i] = (byte)0;
            }
            return bKey;

        }
        #endregion

        #region 对称加密RC2
        /// <summary>
        /// 对称加密RC2
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="strSrc"></param>
        /// <returns></returns>
        public static string EncriptRC2(string strKey, string strSrc)
        {
            RC2 rc2 = RC2.Create();
            rc2.Key = GetRC2Key(strKey);
            rc2.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
            //			rc2.IV=rc2.Key;

            byte[] bytSrc = Encoding.UTF8.GetBytes(strSrc);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, rc2.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(bytSrc, 0, bytSrc.Length);
            cs.FlushFinalBlock();
            string strCripted = Convert.ToBase64String(ms.ToArray());

            ms.Close();
            cs.Close();

            return strCripted;

        }
        #endregion

        #region 对称解密RC2
        /// <summary>
        /// 对称解密RC2
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="strSrc"></param>
        /// <returns></returns>
        public static string DecriptRC2(string strKey, string strSrc)
        {
            RC2 rc2 = RC2.Create();
            rc2.Key = GetRC2Key(strKey);
            rc2.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            byte[] bytSrc = Convert.FromBase64String(strSrc);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, rc2.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(bytSrc, 0, bytSrc.Length);
            string strResult = null;
            try
            {
                cs.FlushFinalBlock();
                strResult = Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {
                throw new Exception("密钥不对");
            }
            finally
            {
                ms.Close();
                cs.Close();
            }
            return strResult;
        }
        #endregion

        #region 不管用户输入多长密码，获得定长128位密钥
        private static byte[] GetRC2Key(string str)
        {
            MD5 md5 = MD5.Create();
            byte[] bReturn = md5.ComputeHash(Encoding.Default.GetBytes(str));
            return bReturn;
        }
        #endregion
    }
}
